# 将注册信息添加至json中
# 单一职责原则：每个类或每个方法只做一件事情。高内聚、低耦合
# 对代码的优化称为重构

acc_info = [{"account":"admin", "password":"123", "realname":"管理员", "balance":0, "phone":"11111111111"},
            {"account":"guest", "password":"guest", "realname":"游客", "balance":0, "phone":"99999999999"}]

class InfoCheck:

    def check_acc(self, account):
        """
        账号要求8位固定长度数字，必填，不能重复
        :return:
        """
        if len(account) != 8 :
            return False

        if not str(account).isdigit() :
            return False

        for acc in acc_info:
            if acc["account"] == account:
                return False
        return True

    def check_pass(self, passwd, cpasswd):
        """
        密码是6位数字，必填，密码和确认密码要一致
        :param passwd:
        :param cpasswd:
        :return:
        """
        if len(passwd) != 6 :
            return False

        if not str(passwd).isdigit() :
            return False

        if passwd != cpasswd:
            return False

        return True

    def check_realname(self):
        """
        必填，长度不能超过20，由字母或中文组成
        :return:
        """
        pass

    def check_uid(self):
        """
        必填，18位数字
        :return:
        """
        pass

    def check_phone(self):
        """
        非必填，如果填了是11位数字
        :return:
        """
        pass

class View:

    def start_menu(self):
        while 1:
            print('1.注册')
            print('2.登录')
            print('3.忘记密码')
            print('4.退出系统')
            choice = input('请选择')
            if choice == '1':
                self.regedit_view()
            elif choice == '2':
                self.login_view()
            elif choice == '3':
                self.get_passwd()
            elif choice == '4':
                exit(0)
            else:
                print('输入错误，请重新输入')

    def regedit_view(self):
        check = InfoCheck()
        print('******欢迎注册******')

        while 1:
            account = input('请输入账号')
            if check.check_acc(account):
                print('账号正确')
                break
            else:
                print('账号错误：必须是数8位字或者该账号已被占用，请重新输入')


        while 1:
            passwd = input('请输入密码')
            confirm_passwd = input('请确认密码')

            if check.check_pass(passwd, confirm_passwd):
                print("密码正确")
                break
            else:
                print("密码错误：要求必须是6位数字，两次密码完全一致。请重新输入")

        uname = input('请输入姓名')
        uid = input('请输入身份证号')
        balance = input('请输入初始余额')
        uphone = input('请输入联系电话')
    #     使用json格式来保存数据
    #   将账号信息添加至存储中
        di = {}
        di["account"] = account
        di["password"] = passwd
        di["realname"] = uname
        di["balance"] = balance
        di["phone"] = uphone
        acc_info.append(di)

    def login_view(self):
        while 1:
            print("****欢迎登录******")
            account = input("请输入账号")
            passwd = input("请输入密码")
            for acc in acc_info:
                if acc["account"] == account:
                    if acc["password"] == passwd:
                        self.show_menu()
                        break
            else:
                print("账号或密码错误，请重新输入")

    def get_passwd(self):
        while 1:
            uname = input("请输入管理员账号")
            upass = input("请输入管理员密码")
            if uname == "admin" and upass == "123":
                while 1:
                    account = input("请输入要找回的密码的账号")
                    for acc in acc_info:
                        if acc["account"] == account:
                            print(f'您的密码是{acc["password"]}')
                            return
                    else:
                        print("您输入的账号有误，请重新输入")
            else:
                choice = input("管理员账号或密码输入错误，重新输入（y）或者返回上一级")
                if choice == 'y':
                    continue
                else:
                    break

    def show_menu(self):
        print("*******欢迎进入主菜单*******")


if __name__ == '__main__':
    v = View()
    v.start_menu()

